package ma.view.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 * The {@code MainFrame} class draws the main JFrame of this application.
 * @author Oliver Eckstein
 *
 */
public class MainFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    
    private JTabbedPane panelCenter;
    private JPanel panelPageEnd;
    private JPanel panelLineStart;
    private JPanel panelPageStart;
    private final String aboutText =
            "<html>E|Benchmark <br>&copy; 2014 Oliver Eckstein</html>";
    
    /**
     * To create an instance of {@code MainFrame}.
     * @param panelCenter the tabbed pane to be shown. 
     */
    public MainFrame(
            JTabbedPane panelCenter,
            JPanel panelPageEnd) {
        this.panelCenter = panelCenter;
        this.panelPageEnd = panelPageEnd;
        createLogoPanel();
        createTitlePanel();
        createAndShowGui();
        createMenuBar();
    }
    
    private void createAndShowGui() {
        BorderLayout layout = new BorderLayout();
        this.setLayout(layout);
        this.add(panelCenter, BorderLayout.CENTER);
        this.add(panelPageEnd, BorderLayout.PAGE_END);                
        this.add(panelLineStart, BorderLayout.LINE_START);
        this.add(panelPageStart, BorderLayout.PAGE_START);
        this.setTitle("E|Benchmark Energy");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        this.setPreferredSize(new Dimension(1000, 500));
        this.pack();
        this.setVisible(true);
    }
    
    private void createLogoPanel() {
        panelLineStart = new JPanel();
        Icon fapsLogo = createImageIcon("/img/logo.jpg");
        JLabel logoLabel = new JLabel(fapsLogo);                
        panelLineStart.add(logoLabel);
    }
    
    private void createTitlePanel() {
        panelPageStart = new JPanel();        
        JLabel title = new JLabel("E|Benchmark Energy");
        title.setFont(title.getFont().deriveFont(14f));
        title.setAlignmentX(LEFT_ALIGNMENT);
        panelPageStart.add(title);
    }
    
    /**
     * Returns a BufferedImage.
     * @param path path of the image.
     * @return BufferedImage of the path.
     * @throws NullPointerException when path was invalid.
     */
    private ImageIcon createImageIcon(String path) {        
        URL resource = MainFrame.class.getResource(path);
        ImageIcon img = null;
        if(resource != null) {
            img = new ImageIcon(resource);                
        } else {
            System.err.println("Couldn't find file: " + path);            
        }
        return img;
    }
    
    // TODO E_ist nicht im Prozessparameterfenster eingeben
    private void createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);
        
        JMenu file = new JMenu("Datei");
        file.getPopupMenu().setLightWeightPopupEnabled(false);
        JMenuItem exit = new JMenuItem("Beenden");
        
        file.add(exit);
        JMenu help = new JMenu("Hilfe");
        help.getPopupMenu().setLightWeightPopupEnabled(false);
        JMenuItem about = new JMenuItem("About E|Benchmark");
        help.add(about);
        
        menuBar.add(file);
        menuBar.add(help);
        
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);                
            }
        });
        
        about.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
             //About Meldung zeigen
               JOptionPane.showMessageDialog(MainFrame.this, aboutText, "Info",
                       JOptionPane.INFORMATION_MESSAGE);
           }
        });
    }
}
